<template>
  <div :class="className" :style="{ height: height }" class="category" />
</template>

<script>
// 环形图

import echarts from 'echarts'
import { debounce } from '@/utils'

require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '370px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart(this.option)
    window.addEventListener('resize', this.resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.resizeHanlder)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(num) {
      this.chart = echarts.init(this.$el, 'macarons')
      const options = {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
          data: [
            'Direct',
            'Marketing',
            'Search Engine',
            'Email',
            'Union Ads',
            'Video Ads',
            'Baidu',
            'Google',
            'Bing',
            'Others'
          ]
        },
        series: [
          {
            name: '产线',
            type: 'pie',
            radius: ['40%', '50%'],
            label: {
              show: false
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 1100, name: 'V-Test-SH' },
              { value: 2300, name: 'V-Test-WX' },
              { value: 4000, name: 'JCET-JY' },
              { value: 1000, name: 'JCET-JYR' },
              { value: 2000, name: 'SH', selected: true }
            ]
          },
          {
            name: '工厂',
            type: 'pie',
            radius: ['60%', '70%'],
            labelLine: {
              length: 15,
              length2: 0,
              maxSurfaceAngle: 80
            },
            label: {
              alignTo: 'edge',
              formatter: '{name|{b}：{d}%}\n{time|{c}}',
              minMargin: 5,
              edgeDistance: 150,
              lineHeight: 15,
              rich: {
                time: {
                  fontSize: 10,
                  color: '#999'
                }
              }
            },
            labelLayout: function(params) {
              console.log(params)
              const isLeft = params.labelRect.x < this.chart.getWidth() / 2
              console.log(isLeft)
              const points = params.labelLinePoints
              // Update the end point.
              points[2][0] = isLeft
                ? params.labelRect.x
                : params.labelRect.x + params.labelRect.width
              return {
                labelLinePoints: points
              }
            },
            data: [
              { value: 3400, name: 'V-Test' },
              { value: 5000, name: 'JCET' },
              { value: 2000, name: 'TF-AMD' }
            ]
          }
        ]
      }
      this.chart.setOption(options)
    },
    resizeHanlder() {
      if (this.chart) {
        this.chart.resize()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.category {
  background: #ffffff;
}
</style>
